layui.config({
    base: 'assets/layuiadmin/' //静态资源所在路径
}).extend({
    index: 'lib/index' //主入口模块
}).use(['index', 'table', 'laytpl'], function(){
    var $ = layui.$,
        table = layui.table;

    table.render({
        elem: '#data-table',
        url: 'channelConfig/list',
        where: getData(),
        toolbar: '#data-table-toolbar',
        title: '落地页列表',
        height: globalHeight,
        page: true,
        cols: [[
            {field: 'id', title: 'ID', width: 60, align: 'center'},
            {field: 'name', title: '名称', width: 120, align: 'center'},
            {field: 'url', title: '地址', align: 'center'},
            {field: 'img', title: '预览图', width: 88, align: 'center', templet: "#imgTpl"},
            {field: 'status', title: '状态', width: 60, align: 'center', templet: "#statusTpl"},
            {field: 'createTime', title: '创建时间', width: 160, align: 'center'},
            {field: 'op', title: '操作', toolbar: '#data-table-bar', width: 120, align: 'center', templet: '#data-table-bar'}
        ]],
        parseData: function(result){
            return {
                "code": result.code, //解析接口状态
                "msg": result.msg, //解析提示文本
                "count": result.data.total, //解析数据长度
                "data": result.data.datas //解析数据列表
            };
        }
    });

    //头工具栏事件
    table.on('toolbar(data-table)', function(obj){
        switch(obj.event){
            case 'search':
                var data =  getData();
                table.reload('data-table', {
                    url: 'channelConfig/list',
                    where: data,
                    page: {
                        curr: 1
                    }
                });
                reloadData(data);
                break;
            case 'add':
                layer.open({
                    type: 2,
                    title: '添加新落地页',
                    offset: '10%',
                    content: 'toEditChannelConfig',
                    area: ['600px', '480px'],
                    btn: ['确定', '取消'],
                    yes: function (index, layero) {     //确定按钮回调
                        var iframeWindow = window['layui-layer-iframe'+ index],
                            submit = layero.find('iframe').contents().find("#data-submit");

                        //监听提交
                        iframeWindow.layui.form.on('submit(data-submit)', function(data){
                            var field = data.field; //获取提交的字段
                            //加载层
                            var load = layerLoad();

                            //提交 Ajax 成功后，静态更新表格中的数据
                            post('channelConfig/save', field, false, function (data) {
                                layer.msg("添加成功");
                                table.reload('data-table');
                                layer.close(index); //关闭弹层
                                layer.close(load);
                            });
                        });

                        submit.trigger('click');
                    }
                });
                break;
        };
    });

    //监听行工具事件
    table.on('tool(data-table)', function(obj){
        var data = obj.data;
        if(obj.event === 'edit'){
            layer.open({
                type: 2,
                title: '编辑落地页',
                offset: '10%',
                content: 'toEditChannelConfig',
                area: ['600px', '480px'],
                btn: ['确定', '取消'],
                success: function(layero, index){   //弹窗成功后回调
                    var iframeWindow = window['layui-layer-iframe' + index];
                    $(iframeWindow.document).find('#id').val(data.id);   //传值到iframe页面
                },
                yes: function (index, layero) {     //确定按钮回调
                    var iframeWindow = window['layui-layer-iframe'+ index],
                        submit = layero.find('iframe').contents().find("#data-submit");

                    //监听提交
                    iframeWindow.layui.form.on('submit(data-submit)', function(data){
                        var field = data.field; //获取提交的字段
                        //加载层
                        var load = layerLoad();

                        //提交 Ajax 成功后，静态更新表格中的数据
                        post('channelConfig/save', field, field, function (data) {
                            layer.msg("更新成功");
                            table.reload('data-table');
                            layer.close(index); //关闭弹层
                            layer.close(load);
                        });
                    });

                    submit.trigger('click');
                }
            });
        }else if(obj.event == 'updateStatus'){
            var msg, status;
            if(data.status == 0){
                msg = '确定禁用该落地页吗？';
                status = 1;
            }else{
                msg = '确定启用该落地页吗？';
                status = 0;
            }
            layer.confirm(msg, {offset: '30%'}, function(index){
                //加载层
                var load = layerLoad();

                post('channelConfig/updateStatus', {id: data.id, status: status}, false, function (res) {
                    obj.update({
                        status:  status,
                        op: 1
                    });
                    layer.close(load);
                    layer.close(index);
                    layer.msg(status == 0 ? '启用成功' : '禁用成功');
                });
            });
        }
    });

    /**
     * 获取查询数据
     * @returns {{host: jQuery, online: jQuery, keyword: (*|jQuery|Array|string|undefined), status: (*|jQuery|Array|string|undefined)}}
     */
    function getData() {
        var status = $('#status').val();

        return {status: status};
    }

    /**
     * 重新渲染数据
     * @param data
     */
    function reloadData(data) {
        $('#status').val(data.status);
    }
});